<template>
  <div>
    <el-dialog
      :title="title"
      center
      :visible.sync="open"
      @close="close()"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item
          :label="$t('batch.systemConfig.form.paramKey')"
          prop="paramKey"
        >
          <el-select
            v-if="paramKey == 1"
            v-model="editForm.paramKey"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.sys_conf_kv_key"
              :key="item.value"
              :label="item.value"
              :value="item.value"
            ></el-option>
          </el-select>
          <el-input
            v-model="editForm.paramKey"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
            v-else
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.systemConfig.form.paramValue')"
          prop="paramValue"
        >
          <el-input
            v-model="editForm.paramValue"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
          />
        </el-form-item>
        <el-form-item :label="$t('common.remark')" prop="remark">
          <el-input
            v-model="editForm.remark"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("common.confirm")
        }}</el-button>
        <el-button @click="close">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { dialogMixin } from "@/libs/dialogMixin";
import {
  getKv,
  saveKv,
  updateKv,
  checkParamKeyUnique,
} from "@/api/batch_app/kvParam";
export default {
  name: "EditKv",
  mixins: [dialogMixin],
  props: {
    typeParamId: {
      type: String,
      default: "",
    },
    paramType: {
      type: Number,
      default: 1,
    },
    // 如果为1则从是系统参数从字典中获得KEY选择
    paramKey: {
      type: String,
      default: "",
    },
  },
  data() {
    // 表单校验
    let checkParamKeyUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(
          new Error(this.$t("batch.systemConfig.rules.paramKeyNotEmpty"))
        );
      } else {
        checkParamKeyUnique({
          typeParamId: this.editForm.typeParamId,
          paramKey: value,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(
                this.$t("batch.systemConfig.rules.paramKeyAlreadyExists")
              )
            );
          } else {
            callback();
          }
        });
      }
    };
    return {
      open: false,
      title: "",
      rules: {
        paramKey: [
          {
            required: true,
            trigger: "change",
            validator: checkParamKeyUniqueRule,
          },
        ],
        paramValue: [
          {
            required: true,
            message: this.$t("batch.systemConfig.rules.paramValueNotEmpty"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    show(id = "", typeParamId, disabled = false) {
      this.$set(this, "editForm", {});
      if (id) {
        getKv({
          id: id,
        }).then((res) => {
          this.$set(this, "editForm", res.result);
          this.editForm.typeParamId = typeParamId;
          this.editForm.paramType = this.paramType;
        });
        this.title = this.$t("common.edit");
      } else {
        this.title = this.$t("common.add");
        this.editForm.typeParamId = typeParamId;
        this.editForm.paramType = this.paramType;
        this.loadDicts(["sys_conf_kv_key"]);
      }
      this.open = true;
    },
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          if (this.editForm.id) {
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            updateKv(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.modifySuccess"));
              this.open = false;
              this.$emit("editComplete", true);
            });
          } else {
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            saveKv(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.addSuccess"));
              this.open = false;
              this.$emit("editComplete", true);
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped>
</style>
